package com.itheima.controller;

import com.itheima.service.CommentService;
import com.itheima.service.SmallVideosService;
import com.itheima.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/smallVideos")
public class SmallVideosController {

    @Autowired
    private SmallVideosService videosService;

    @Autowired
    private CommentService commentService;

    /**
     * 发布评论
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity saveComments(@RequestBody Map map) {
        String movementId = (String )map.get("movementId");
        String comment = (String)map.get("comment");
        commentService.saveComments(movementId,comment);
        return ResponseEntity.ok(null);
    }

    /**
     * 发布视频
     */
    @PostMapping
    public ResponseEntity saveVideos(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        videosService.saveVideos(videoThumbnail,videoFile);
        return ResponseEntity.ok(null);
    }

    //视频点赞
    @PostMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String id){
        Integer like = videosService.like(id);
        return ResponseEntity.ok(like);
    }

    //视频点赞删除
    @PostMapping("/{id}/dislike")
    public ResponseEntity disLike(@PathVariable("id") String disId){
        Integer disLike = videosService.disLike(disId);
        return ResponseEntity.ok(disId);
    }

    //视频用户关注
    @GetMapping("{uid}/userFocus")
    public ResponseEntity<Void> likeUser(@PathVariable("uid") Long uid) {
        this.videosService.likeUser(uid);
        return ResponseEntity.ok(null);
    }

    //视频用户取消关注
    @GetMapping("{uid}/userUnFocus")
    public ResponseEntity<Void> notLikeUser(@PathVariable("uid") Long likeUserId) {
        this.videosService.notLikeUser(likeUserId);
        return ResponseEntity.ok(null);
    }

    //评论列表
    @GetMapping
    public ResponseEntity findSmallVideos(@RequestParam(defaultValue = "1") Integer page,
                                          @RequestParam(defaultValue = "10")Integer pagesize,
                                          String movementId){
        PageResult pr =videosService.findSmallVideos(page,pagesize,movementId);
        return ResponseEntity.ok(pr);

    }

/*    //发布评论
    @PostMapping("/{id}/comments")
    public ResponseEntity comments(@PathVariable("id") String id,@RequestBody String comment) {
        videosService.savecomments(id, comment);
        return ResponseEntity.ok(null);
    }*/

    /**
     * 评论喜欢点赞
     */
    @GetMapping("/{id}/like")
    public ResponseEntity like1(@PathVariable("id") String commentsId){
        Integer integer = videosService.commentsLike(commentsId);
        return ResponseEntity.ok(integer);
    }

    /**
     * 评论取消点赞
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String commentsId) {
        Integer likeCount = videosService.commentsLike2(commentsId);
        return ResponseEntity.ok(likeCount);
    }


}